/**
 * 秘钥管理appSecret.js
 * 版本号： v1.0.0
 */
$(function(){
        $("#appSecretDg").datagrid({
            fit:true,
            fitColumns: true,
            method: "POST",
            rownumbers: true,
            singleSelect:true,
            striped:true,
            pagination: true,
            border:false,
            pageSize:10,
            pageList:[10,30,50,100],
            url: CONTEXT_PATH + '/appSecret/findPage.do',
            idField: 'id',
            striped:true,
            frozenColumns:[[
                {field:'id',checkbox:true}
            ]],
            columns: [
                [
                    {field: 'appKey', title: '应用KEY', width: 85, align: 'center', fixed: true},
                    {field: 'secret', title: '应用密钥', width: 250, align: 'center', fixed: true},
                    {field: 'limit',  title: '限制次数', width: 70, align: 'center', fixed: true},
                    {field: 'status', title: '是否启用', width: 70, align: 'center', fixed: true,formatter:function(val){
                        if(val == 1) return "<span style='color: green; font-weight: bold'>是</span>";
                        if(val == 0) return "<span style='color: red; font-weight: bold'>否</span>";
                    }},
                    {field: 'createDate', title: '创建时间', fit: true, width: 130}
                ]
            ],
            onLoadSuccess:function(data){
                if(data.rows.length > 0){
                    $("#appSecretDg").datagrid("selectRow", 0);
                }
            }
        });

    //定义可操作按钮
    $("#appSecret_Menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            appSecret_add();
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            appSecret_del();
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            appSecret_edit();
                        }
                    });
                }
                //重置按钮
                if("resetBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            appSecret_restAppKey();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            appSecret_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
                //计数清零
                if("clearBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            appSecret_clearCount();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加秘钥
 */
function appSecret_add(){
    $("#appSecret_dialog").dialog({
        title: '添加秘钥',
        width: 420,
        modal: true,
        closed: true,
        height: 260,
        iconCls:"icon-custom-add",
        buttons:[{
            text:"添加",
            iconCls:"icon-role-state-ok",
            handler:function(){
                doOptionAppSecret();
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#appSecret_dialog').dialog('close');
            }
        }]
    });
    $("#id").val("");
    $("#appKey").textbox('setValue',"");
    $("#secret").textbox('setValue',"");
    $("#appSecret_dialog").dialog("move",{left:450,top:100});
    $("#appSecret_dialog").dialog('open');

}

/**
 * 删除秘钥
 */
function appSecret_del(){
    var row = $("#appSecretDg").datagrid("getSelected");
    if(row == null){
        $.messager.show({ title:'操作提示', msg:"请选择删除秘钥", timeout:3000, showType:"slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.appKey + ' 应用秘钥吗？', function(r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/appSecret/del.do?id=" + row.id,
                type:"get",
                success:function(data){
                    messager(data);
                    $("#appSecretDg").datagrid("reload",{});
                }
            })
        }
    });

}

/**
 * 修改秘钥
 * @returns {boolean}
 */
function appSecret_edit(){
    var row = $("#appSecretDg").datagrid("getSelected");
    if(row == null){
        $.messager.show({ title:'操作提示', msg:"请选择修改秘钥", timeout:3000, showType:"slide" });
        return;
    }
    $("#appSecret_dialog").dialog({
        title: '修改秘钥',
        width: 420,
        modal: true,
        closed: true,
        height: 260,
        iconCls:"icon-custom-edit",
        buttons:[{
            text:"修改",
            iconCls:"icon-role-state-ok",
            handler:function(){
                doOptionAppSecret();
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#appSecret_dialog').dialog('close');
            }
        }]
    });

    $('#appSecret_dialog').dialog('open');
    $("#appSecret_dialog").dialog("move",{left:450,top:100});
    $("#appSecretForm").form("load", row);
}

/**
 * 秘钥详情
 */
function appSecret_view(iconCls){
    var row = $("#appSecretDg").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的秘钥详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(myxq.id, myxq.url + row.id, iconCls, myxq.closable, myxq.title);
}


/**
 * 执行操作秘钥
 */
function doOptionAppSecret(){
    var params = $("#appSecretForm").serialize();
    $("#appSecretForm").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/appSecret/save.do",
        onSubmit:function() {
            return $(this).form("validate");
        },
        success:function(data) {
            var obj = JSON.parse(data);
            messager(obj);
            $('#appSecret_dialog').dialog('close');
            $("#appSecretDg").datagrid("reload",{});
        }
    });
}

/**
 * 重置秘钥
 */
function appSecret_restAppKey(){
    $.ajax({
        url: CONTEXT_PATH + "/appSecret/restAppKeySecret.do",
        type:"get",
        success:function(data){
            //var obj = JSON.parse(data);
            messager(data);
        }
    })

}

/**
 * 计数器清零
 */
function appSecret_clearCount(){
    var row = $("#appSecretDg").datagrid("getSelected");
    if(row == null){
        $.messager.show({ title:'操作提示', msg:"请选择修改秘钥", timeout:3000, showType:"slide" });
        return;
    }
    $.ajax({
        url: CONTEXT_PATH + "/appSecret/resetAppCallAllCount.do?appKey=" + row.appKey,
        type:"get",
        success:function(data){
            //var obj = JSON.parse(data);
            messager(data);
        }
    })

}